--
-- Author: gnar
-- Date: 2016-01-04 16:44:33
--
local AStar = import(".AStar")
local ManagerMap = class("ManagerMap")
local gInstance = nil

function ManagerMap.getInstance()
	if not gInstance then
		gInstance = ManagerMap.new()
	end

	return gInstance
end


function ManagerMap.getMapNode(index)
	--local picName = "maps/" .. index .. ".jpg"
	local tmxName = "maps/" .. index .. ".tmx"
	local map = cc.TMXTiledMap:create(tmxName)
	--local bg = cc.Sprite:create(picName):addTo(map):align(cc.p(0,0), 0, 0)
	--local bg = display.newSprite(filename, x, y, params)
	map:setScale(1.5)
	return map

end



-----------------------------------------------------
function ManagerMap:init(map, camera)
	self.map = map
	self.camera = camera
end

function ManagerMap:setPlayer(player)
	self.player = player
end

-- function ManagerMap:getTileSize()
-- 	return self.map:getTileSize()
-- end

-- function ManagerMap:getMapSize()
-- 	return self.map:getMapSize()
-- end


function ManagerMap:getMapSizePx()
	local mapSize = self.map:getMapSize()
	local tileSize = self.map:getTileSize()
	return cc.size(mapSize.width * tileSize.width, mapSize.height * tileSize.height)
end
	

function ManagerMap:pos2px(pos)
	local tileSize = self.map:getTileSize()
	local mapSize = self.map:getMapSize()

	return cc.p(tileSize.width*pos.x - tileSize.width/2, (mapSize.height - pos.y) * tileSize.height + tileSize.height/2)
end

function ManagerMap:px2pos(posPx)
	local tileSize = self.map:getTileSize()
	local mapSize = self.map:getMapSize()

	return cc.p(math.ceil(posPx.x/tileSize.width), mapSize.height + 1 - math.ceil(posPx.y/tileSize.height))
end


function ManagerMap:getOrientation(toPos, fromPos)-- toPos fromPos同一坐标系即可

	fromPos = fromPos or self.player:getMapPos()
	--print("test 123213312312",math.acos(cos),angle,toPos.x , fromPos.x )
	local angle = self:getAngle(toPos, fromPos)
	local orientation
	if angle >= 337.5 or angle <= 22.5 then
		orientation = Constants.rightOrientation
	elseif angle >= 22.5 and angle <= 67.5 then
		orientation = Constants.backRightOrientation
	elseif angle >= 67.5 and angle <= 112.5 then
		orientation = Constants.backOrientation
	elseif angle >= 112.5 and angle <= 157.5 then
		orientation = Constants.backLeftOrientation
	elseif angle >= 157.5 and angle <= 202.5 then
		orientation = Constants.leftOrientation
	elseif angle >= 202.5 and angle <= 247.5 then
		orientation = Constants.fowardLeftOrientation	
	elseif angle >= 247.5 and angle <= 292.5 then
		orientation = Constants.fowardOrientation
	elseif angle >= 292.5 and angle <= 337.5 then
		orientation = Constants.fowardRightOrientation
	end

	return orientation
end


function ManagerMap:findPath(endPoint, startPoint)

	startPoint = startPoint or self.player:getMapPos()
	local roadGird = self:getRoadGird()
	--local path = AStar.findPath(startPoint, endPoint, nil, self.map:getMapSize().width, self.map:getMapSize().height)
	local path = AStar.newFindPath(startPoint, endPoint, roadGird, self.map:getMapSize().width, self.map:getMapSize().height)


	return path
end

function ManagerMap:getRoadGird()
	if not self.map.roadGird then
		self.map.roadGird = {}
		local layer = self.map:getLayer("road")
		local mapSize = self.map:getMapSize()
		local grid = self.map.roadGird
		local line

		for y = 1, mapSize.height do
			grid[y] = grid[y] or {}
			line = grid[y]
			for x = 1, mapSize.width do
				if 0 ~= layer:getTileGIDAt(cc.p(x - 1, y - 1)) then
					line[x] = true
					-- printInfo("tile pos (%d,%d)", x, y)
				end
			end
		end
	end

	return self.map.roadGird
end

function ManagerMap:getBarrierGird()

end

function ManagerMap:getAngle(toPos, fromPos)-- toPos fromPos同一坐标系即可
	fromPos = fromPos or self.player:getMapPos()
	--fromPos 为原点 屏幕坐标系方向 建立坐标系 
	local relativeToPos = cc.p(toPos.x - fromPos.x,toPos.y - fromPos.y)

	local cos = relativeToPos.x/ math.sqrt( (math.pow(relativeToPos.x, 2) + math.pow(relativeToPos.y, 2)) )
	local angle = math.acos(cos)/math.pi * 180  
	if relativeToPos.y / math.abs(relativeToPos.y) > 0 then
		angle = 360 - angle
	end
	return angle -- 0-360
end

function ManagerMap:walkToPos(toPos, fromPos, player) -- toPos fromPos地图中的位置
	fromPos = fromPos or self.player:getMapPos()
	player = player or self.player

	local mapPos = self:px2pos(toPos)
	local roadGird = self:getRoadGird()
	if roadGird[mapPos.y][mapPos.x] then
		--local view = player:getView()
		player:walkToPos(mapPos)
	end	
end

function ManagerMap:walkToPosByAngle(toPos, fromPos, player)

end

return ManagerMap
